package com.jy.adscm.wechat.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.wechat.bean.LoginUser;
import com.jy.adscm.wechat.bean.WXSessionModel;
import com.jy.adscm.wechat.bean.WechatUser;
import com.jy.adscm.wechat.service.WechatUserService;
import com.jy.adscm.wechat.utils.HttpClientUtil;
import com.jy.adscm.wechat.utils.IMoocJSONResult;
import com.jy.adscm.wechat.utils.JsonUtils;
import com.jy.adscm.wechat.utils.RedisOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author zyl
 * @Date 2019/9/23 -10:07
 */
@RestController
@RequestMapping("/adscm")
public class WeChatLoginController {

    @Autowired
    private RedisOperator redis;
    @Autowired
    WechatUserService wechatUserService;
    @CrossOrigin(allowCredentials = "true")
    @RequestMapping("/appLogin")
    public  SimpleCoreResponse  login(@RequestBody LoginUser loginUser)throws AdscmSystemException {
        String sessionKey=    wechatUserService.login(loginUser);
        return  new SimpleCoreResponse("200","成功登录",sessionKey);
    }
    @CrossOrigin(allowCredentials = "true")
    @RequestMapping("/appLogout")
    public  SimpleCoreResponse  logout(HttpServletRequest request)throws AdscmSystemException {
        String sessionKey = request.getParameter("sessionKey");
        Boolean result=    wechatUserService.loginout(sessionKey);
        return  new SimpleCoreResponse("200","成功退出",result);
    }
    @PostMapping("/wxLogin")
    public SimpleCoreResponse  wxLogin(String code) {
        System.out.println("wxlogin - code: " + code);
        String url = "https://api.weixin.qq.com/sns/jscode2session";
        Map<String, String> param = new HashMap<>();
        param.put("appid", "wx935bd4e3cf2ea7aa");
        param.put("secret", "174ffe95e665931ea89bc3b55e4ced0b");
        param.put("js_code", code);
        param.put("grant_type", "authorization_code");

        String wxResult = HttpClientUtil.doGet(url, param);
        System.out.println(wxResult);

        WXSessionModel model = JsonUtils.jsonToPojo(wxResult, WXSessionModel.class);
        model.setSession_key(UUID.randomUUID().toString().replaceAll("-",""));
        //先根据openid获取上一次的登录信息，删除上一次的登录信息
        String sessionKey = redis.get(model.getOpenid());
        if (AdscmBaseUtils.getInstance().isNotEmpty(sessionKey,null)){
            redis.del(sessionKey);
        }
        redis.set(model.getSession_key(),model.getOpenid(),
                2*60*60*1000); //该次回话量小时过期
        redis.set(model.getOpenid(),model.getSession_key());
        //判断是否是新用户，如果是用户跳转到登录页面进行用户信息的填写 根据openid查询是否存在该用户的信息

        WechatUser wechatUser = wechatUserService.selectByOpenId(model.getOpenid());
        if (!AdscmBaseUtils.getInstance().isNotEmpty(wechatUser,null)){
            return new SimpleCoreResponse("300","新人第一登录,请跳转到登录页面",model.getSession_key());
        }
        System.out.println( model.getOpenid()+model.getSession_key());
        return new SimpleCoreResponse("200","成功登录",model.getSession_key());
    }

}